package com.siruier.boss.ui.base

import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.core.app.ComponentActivity
import androidx.fragment.app.FragmentActivity
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.viewBinding
import com.siruier.boss.ui.helper.ILoa
import com.siruier.boss.ui.helper.IMsa
import com.siruier.boss.ui.helper.loa
import com.siruier.boss.ui.helper.msa
import com.yanzhenjie.sofia.Utils
import java.lang.reflect.ParameterizedType

typealias ActivityForResult = (ActivityResult) -> Unit
typealias RequestMultiplePermissions = (Map<String, Boolean>) -> Unit

open class BaseActivity<VB : ViewBinding> : FragmentActivity(), View.OnClickListener, IMsa by msa(), ILoa by loa() {
    protected val vb by viewBinding(::loadViewBinding)
    protected lateinit var mThis: BaseActivity<VB>

    fun bindViewClick(vararg views: View) = com.siruier.boss.ui.ext.bindViewClick(this, *views)

    private fun loadViewBinding(view: ComponentActivity): VB {
        val clz = (this::class.java.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<ViewBinding>
        val method = clz.getDeclaredMethod("inflate", LayoutInflater::class.java)
        return method.invoke(null, layoutInflater) as VB
    }

    open fun loadView(load: VB.() -> Unit) = vb.also(load)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initManageStartActivity()
        initLifecycleOwnerActivity()
        Utils.setStatusBarDarkFont(this, true)
        mThis = this
        setContentView(vb.root)
        init()
    }

    private fun init() {
        initView()
        initData()
    }

    open fun initView() {}
    open fun initData() {}
    open fun doFinish() {
        finish()
    }

    override fun onClick(v: View?) {

    }

    open fun loginChange(intent: Intent) {

    }

    override fun getResources(): Resources {
        //AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()))
        return super.getResources()
    }
}